<template>
  <div class="dashboard-container">
    <div class="welcome-section">
      <div class="welcome-left">
        <h1>{{ greeting }}，{{ username }}</h1>
        <p class="subtitle">今天是 {{ currentDate }}</p>
      </div>
      <div class="welcome-right">
        <el-button type="primary" @click="createTask">
          <el-icon><Plus /></el-icon>新建备课
        </el-button>
      </div>
    </div>

    <el-row :gutter="20">
      <el-col :span="6" v-for="stat in statsData" :key="stat.title">
        <div class="stat-card" :class="stat.type">
          <div class="stat-icon">
            <el-icon><component :is="stat.icon" /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ stat.number }}</div>
            <div class="stat-title">{{ stat.title }}</div>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="dashboard-main">
      <el-col :span="16">
        <el-card class="recent-tasks" shadow="hover">
          <template #header>
            <div class="card-header">
              <h2>最近备课计划</h2>
              <el-button type="primary" link @click="viewAllTasks">
                查看全部<el-icon class="el-icon--right"><ArrowRight /></el-icon>
              </el-button>
            </div>
          </template>
          <div class="task-list">
            <div v-for="task in recentTasks" :key="task.id" class="task-item">
              <div class="task-info">
                <div class="task-title">
                  <span>{{ task.title }}</span>
                  <el-tag :type="task.status === 'completed' ? 'success' : 'warning'" size="small">
                    {{ task.status === 'completed' ? '已完成' : '进行中' }}
                  </el-tag>
                </div>
                <div class="task-meta">
                  <span>{{ task.subject }} | {{ task.grade }}</span>
                  <span>创建于: {{ task.createTime }}</span>
                </div>
              </div>
              <div class="task-progress">
                <el-progress
                  :percentage="task.progress"
                  :status="task.progress === 100 ? 'success' : ''"
                  :stroke-width="8"
                />
              </div>
              <div class="task-actions">
                <el-button type="primary" link @click="continueTask(task)"> 继续备课 </el-button>
                <el-button type="info" link @click="previewTask(task)"> 预览 </el-button>
              </div>
            </div>
          </div>
        </el-card>

        <el-card class="quick-actions" shadow="hover">
          <template #header>
            <div class="card-header">
              <h2>快捷操作</h2>
            </div>
          </template>
          <div class="actions-grid">
            <div
              v-for="action in quickActions"
              :key="action.title"
              class="action-item"
              @click="action.onClick"
            >
              <el-icon><component :is="action.icon" /></el-icon>
              <span>{{ action.title }}</span>
            </div>
          </div>
        </el-card>
      </el-col>

      <el-col :span="8">
        <el-card class="calendar-card" shadow="hover">
          <div class="calendar-container">
            <teaching-calendar @date-click="handleDateClick" @event-click="handleEventClick" />
          </div>
        </el-card>

        <el-card class="tips-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <h2>备课小贴士</h2>
            </div>
          </template>
          <div class="tips-content">
            <el-carousel height="150px" direction="vertical" :autoplay="true" :interval="5000">
              <el-carousel-item v-for="tip in teachingTips" :key="tip.id">
                <div class="tip-item">
                  <el-icon :class="tip.type"><component :is="tip.icon" /></el-icon>
                  <p>{{ tip.content }}</p>
                </div>
              </el-carousel-item>
            </el-carousel>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, reactive, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import {
  HomeFilled,
  Plus,
  Clock,
  Document,
  Collection,
  Notebook,
  VideoPlay,
  Picture,
  Link,
  ArrowRight,
  Edit,
  VideoCamera,
  Reading,
  Bell,
  Histogram,
  Calendar,
  List,
  Setting,
  User,
  PieChart,
  Medal,
  Promotion,
} from '@element-plus/icons-vue'
import type { Resource } from '@/types'
import TeachingCalendar from '@/components/calendar/TeachingCalendar.vue'
const params = new URLSearchParams(window.location.search)
export default defineComponent({
  name: 'DashboardView',
  components: {
    HomeFilled,
    Plus,
    Clock,
    Document,
    Collection,
    Notebook,
    VideoPlay,
    Picture,
    Link,
    ArrowRight,
    Edit,
    VideoCamera,
    Reading,
    Bell,
    Histogram,
    Calendar,
    List,
    Setting,
    User,
    PieChart,
    Medal,
    Promotion,
    TeachingCalendar,
  },
  setup() {
    const router = useRouter()
    const username = ref(localStorage.getItem('username') || '老师')
    const currentDate = ref(new Date())
    const calendarView = ref('month')

    // 获取时间问候语
    const greeting = computed(() => {
      const hour = new Date().getHours()
      if (hour < 6) return '凌晨好'
      if (hour < 9) return '早上好'
      if (hour < 12) return '上午好'
      if (hour < 14) return '中午好'
      if (hour < 17) return '下午好'
      if (hour < 19) return '傍晚好'
      return '晚上好'
    })

    // 统计数据
    const statsData = ref([
      { title: '备课计划', number: 12, type: 'primary', icon: 'Document' },
      { title: '教学设计', number: 8, type: 'success', icon: 'Collection' },
      { title: '练习题', number: 36, type: 'warning', icon: 'Edit' },
      { title: '教学资源', number: 24, type: 'info', icon: 'VideoCamera' },
    ])

    // 最近任务列表
    const recentTasks = ref([
      {
        id: '1',
        title: '七年级语文《春》课程设计',
        subject: '语文',
        grade: '七年级',
        status: 'in_progress',
        progress: 60,
        createTime: '2024-03-10',
      },
      {
        id: '2',
        title: '九年级数学《二次函数》教学计划',
        subject: '数学',
        grade: '九年级',
        status: 'in_progress',
        progress: 30,
        createTime: '2024-03-08',
      },
      {
        id: '3',
        title: '八年级英语《Unit 5》备课',
        subject: '英语',
        grade: '八年级',
        status: 'completed',
        progress: 100,
        createTime: '2024-03-05',
      },
    ])

    // 推荐资源
    const recommendedResources = ref<Resource[]>([
      {
        id: '1',
        task_id: '',
        title: '趣味数学互动课件',
        type: 'document',
        url: '#',
        created_at: '',
        subject: '数学',
        grade: '七年级',
      } as any,
      {
        id: '2',
        task_id: '',
        title: '物理实验视频合集',
        type: 'video',
        url: '#',
        created_at: '',
        subject: '物理',
        grade: '八年级',
      } as any,
      {
        id: '3',
        task_id: '',
        title: '古诗词配图素材',
        type: 'image',
        url: '#',
        created_at: '',
        subject: '语文',
        grade: '九年级',
      } as any,
    ])

    // 快捷操作
    const quickActions = ref([
      {
        title: '创建备课',
        icon: 'Plus',
        onClick: () => router.push('/task/create'),
      },
      {
        title: '教学设计',
        icon: 'Collection',
        onClick: () => router.push('/teaching-design'),
      },
      {
        title: '习题管理',
        icon: 'Edit',
        onClick: () => router.push('/exercises'),
      },
      {
        title: '资源库',
        icon: 'VideoCamera',
        onClick: () => router.push('/resources'),
      },
      {
        title: '数据分析',
        icon: 'Histogram',
        onClick: () => router.push('/analysis'),
      },
      {
        title: '历史记录',
        icon: 'List',
        onClick: () => router.push('/history'),
      },
    ])

    // 教学小贴士
    const teachingTips = ref([
      {
        id: 1,
        content: '制定明确的教学目标，帮助学生更好地理解和掌握知识点',
        type: 'success',
        icon: 'Reading',
      },
      {
        id: 2,
        content: '合理安排课堂时间，注意教学节奏和学生的接受程度',
        type: 'warning',
        icon: 'Bell',
      },
      {
        id: 3,
        content: '多准备一些课堂互动环节，提高学生的参与度',
        type: 'info',
        icon: 'Collection',
      },
    ])

    // 检查日期是否有任务
    const hasTaskOnDate = (date: any) => {
      // 这里可以根据实际数据判断该日期是否有任务
      return Math.random() > 0.7
    }

    // 继续备课
    const continueTask = (task: any) => {
      router.push(`/teaching-design/${task.id}`)
    }

    // 预览任务
    const previewTask = (task: any) => {
      router.push(`/teaching-design/${task.id}/preview`)
    }

    // 处理日期点击事件
    const handleDateClick = (date: Date) => {
      currentDate.value = date
      console.log('选择的日期:', date)
    }

    // 处理事件点击
    const handleEventClick = (events: any[]) => {
      console.log('日期上的事件:', events)
      // 这里可以显示事件详情或进行其他操作
    }

    // 新增方法
    const createTask = () => {
      router.push('/task/create')
    }

    const viewAllTasks = () => {
      router.push('/task/create')
    }

    onMounted(async () => {
      // 这里应该是实际的API调用
      // 模拟获取数据
      await new Promise((resolve) => setTimeout(resolve, 500))
    })

    return {
      username,
      greeting,
      statsData,
      recentTasks,
      recommendedResources,
      currentDate,
      calendarView,
      quickActions,
      teachingTips,
      hasTaskOnDate,
      continueTask,
      previewTask,
      handleDateClick,
      handleEventClick,
      createTask,
      viewAllTasks,
    }
  },
})
</script>

<style scoped>
.dashboard-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

h1 {
  margin-bottom: 24px;
  color: #333;
}

.welcome-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.welcome-left h1 {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.subtitle {
  margin: 8px 0 0;
  color: #909399;
  font-size: 14px;
}

.stat-card {
  display: flex;
  align-items: center;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
  transition: transform 0.3s;
}

.stat-card:hover {
  transform: translateY(-5px);
}

.stat-card.primary .stat-icon {
  color: #409eff;
}
.stat-card.success .stat-icon {
  color: #67c23a;
}
.stat-card.warning .stat-icon {
  color: #e6a23c;
}
.stat-card.info .stat-icon {
  color: #909399;
}

.stat-icon {
  font-size: 48px;
  margin-right: 16px;
}

.stat-content {
  flex: 1;
}

.stat-number {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  line-height: 1;
  margin-bottom: 8px;
}

.stat-title {
  font-size: 14px;
  color: #909399;
}

.dashboard-main {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h2 {
  margin: 0;
  font-size: 18px;
  color: #303133;
}

.task-item {
  padding: 16px;
  border-bottom: 1px solid #ebeef5;
}

.task-item:last-child {
  border-bottom: none;
}

.task-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.task-title span {
  font-size: 16px;
  color: #303133;
  font-weight: 500;
}

.task-meta {
  display: flex;
  justify-content: space-between;
  color: #909399;
  font-size: 14px;
  margin-bottom: 12px;
}

.task-progress {
  margin: 12px 0;
}

.task-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 12px;
}

.quick-actions {
  margin-top: 20px;
}

.actions-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.action-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  background-color: #f5f7fa;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
}

.action-item:hover {
  background-color: #ecf5ff;
  color: #409eff;
}

.action-item .el-icon {
  font-size: 24px;
  margin-bottom: 8px;
}

.calendar-card {
  margin-bottom: 20px;
}

.calendar-container {
  padding: 0;
}

.calendar-cell {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  padding: 4px 0;
}

.task-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: #409eff;
}

.tips-card {
  margin-top: 20px;
}

.tip-item {
  padding: 0 20px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.tip-item .el-icon {
  font-size: 24px;
}

.tip-item.success .el-icon {
  color: #67c23a;
}
.tip-item.warning .el-icon {
  color: #e6a23c;
}
.tip-item.info .el-icon {
  color: #909399;
}

.tip-item p {
  margin: 0;
  font-size: 14px;
  color: #606266;
  line-height: 1.6;
}
</style>
